<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        /* li:nth-child(2n){
           background-color: red;
        }
        li:nth-child(2n+1){
            background-color: blue;
        }
        li:hover{
            background-color: aquamarine;
        } */
    </style>
</head>
<body>
    <ul>
        <li>aaa</li>
        <li>aaa</li>
        <li>aaa</li>
        <li>aaa</li>
        <li>aaa</li>
        <li>aaa</li>
        <li>aaa</li>
        <li>aaa</li>
    </ul>
    <script>
        var lis=document.getElementsByTagName("li");

        for(var i=0;i<lis.length;i++){
             if(i%2==0){//i除以2，余数为0，偶数
                lis[i].style.backgroundColor='red';
             }else{//奇数
                lis[i].style.backgroundColor='blue';
             }

            lis[i].onmouseover=function(){
                //console.dir(this);//触发事件的元素
                //滑过的时候，先将以前的颜色存储起来
                this.oldColor=this.style.backgroundColor;
                //之后改变颜色
                this.style.backgroundColor="pink";
            } 

            lis[i].onmouseout=function(){
                //离开的时候，将存储的颜色设置为背景颜色
                this.style.backgroundColor=this.oldColor;
            } 
        }

        // lis[0].onmouseover=function(){
        //   //console.log(this);//触发事件的元素
        //   this.style.backgroundColor="pink";
        // }
    </script>
</body>
</html>